1 Goal of the script

This script plots the data from the edge angle analysis (archaeology) in order to visualizes the measurements.
The plots will also visualize the differences between the three methods.

dir_in <- "analysis/derived_data/"
dir_out <- "analysis/plots"

Raw data must be located in ~/analysis/derived_data/.
Formatted data will be saved in ~/analysis/plots. The knit directory for this script is the project directory.


2 Load packages

pack_to_load <- c("R.utils", "tools", "openxlsx", "ggplot2", "tidyverse", "patchwork", "doBy", "ggrepel")
sapply(pack_to_load, library, character.only = TRUE, logical.return = TRUE)
Warning: package 'R.utils' was built under R version 4.1.3
Warning: package 'R.oo' was built under R version 4.1.3
Warning: package 'R.methodsS3' was built under R version 4.1.3
Warning: package 'openxlsx' was built under R version 4.1.3
Warning: package 'ggplot2' was built under R version 4.1.3
Warning: package 'tidyverse' was built under R version 4.1.3
Warning: package 'tibble' was built under R version 4.1.3
Warning: package 'tidyr' was built under R version 4.1.3
Warning: package 'readr' was built under R version 4.1.3
Warning: package 'dplyr' was built under R version 4.1.3
Warning: package 'stringr' was built under R version 4.1.3
Warning: package 'forcats' was built under R version 4.1.3
Warning: package 'patchwork' was built under R version 4.1.3
Warning: package 'doBy' was built under R version 4.1.3
  R.utils     tools  openxlsx   ggplot2 tidyverse patchwork      doBy   ggrepel 
     TRUE      TRUE      TRUE      TRUE      TRUE      TRUE      TRUE      TRUE 

3 Get name, path and information of the file

data_file <- list.files(dir_in, pattern = "\\.Rbin$", full.names = TRUE)
md5_in <- md5sum(data_file)
info_in <- data.frame(file = basename(names(md5_in)), checksum = md5_in, row.names = NULL)

4 Load data into R object

imp_data <- loadObject(data_file)
str(imp_data)
'data.frame':   58516 obs. of  11 variables:
 $ Site           : chr  "Balve" "Balve" "Balve" "Balve" ...
 $ ID             : chr  "MU-022" "MU-022" "MU-022" "MU-022" ...
 $ Tool_type      : chr  "KM_dex" "KM_dex" "KM_dex" "KM_dex" ...
 $ Section        : num  1 1 1 1 1 1 1 1 1 1 ...
 $ Edge           : chr  "E1" "E1" "E1" "E1" ...
 $ Angle_number   : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Distance_origin: num  1 2 3 4 5 6 7 8 9 10 ...
 $ Segment        : num  2 2 2 2 2 2 2 2 2 2 ...
 $ Three_point    : num  158.6 137.6 124 105.4 89.5 ...
 $ Two_lines      : num  137.9 106.4 71.6 41.4 35.9 ...
 $ Best_fit       : num  138.8 107.7 70.5 42.2 30.8 ...

The imported file is: “~/analysis/derived_data/EdgeAngle.Rbin”

5 Define numeric variables

num.var <- 9:length(imp_data)

6 Plot edge angle measurements

6.1 Plots showing the angles as lines

# split the data in the individual 227 samples
sp <- split(imp_data, imp_data[["ID"]])

for (j in seq_along(sp)) {
  p <- vector(mode = "list", length = length(num.var))
  names(p) <- names(sp[[j]])[num.var]

  for (i in seq_along(num.var)){ 
    p[[i]] <- ggplot(data = sp[[j]], aes_string(y = "Section",
              x = names(sp[[j]])[num.var[i]], colour = "Angle_number")) +
              geom_line(aes(group = Angle_number), orientation = "y") + 
              geom_hline(yintercept = c(2, 9), linetype = "dashed") +
              facet_wrap(~ Edge) +
              scale_colour_continuous(trans = "reverse") +
              scale_y_continuous(breaks=1:10, trans = "reverse") +
              labs(y = "Section") + 
              labs(colour = "Dis. origin (mm)") + 
              labs(x = gsub("_", " ", names(sp[[j]])[num.var[i]])) + 
              scale_x_continuous(n.breaks = 3) +
                theme_classic() 
  }

wp <- wrap_plots(p) + plot_annotation(title = names(sp)[j]) + 
      plot_layout(guides = "collect")

print(wp)

  # save to PDF
  file_out <- paste0(file_path_sans_ext(info_in[["file"]]), "_EA_plot_", names(sp)[j],
                     ".pdf")
  ggsave(filename = file_out, plot = wp, path = dir_out, 
         device = "pdf", width = 260, height = 170, units = "mm")
}  

The files will be saved as “~/analysis/plots.[ext]”.


7 sessionInfo() and RStudio version

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] ggrepel_0.9.1     doBy_4.6.13       patchwork_1.1.2   forcats_0.5.2    
 [5] stringr_1.4.1     dplyr_1.0.10      purrr_0.3.4       readr_2.1.2      
 [9] tidyr_1.2.1       tibble_3.1.8      tidyverse_1.3.2   ggplot2_3.3.6    
[13] openxlsx_4.2.5    R.utils_2.12.0    R.oo_1.25.0       R.methodsS3_1.8.2

loaded via a namespace (and not attached):
 [1] httr_1.4.4           sass_0.4.2           jsonlite_1.8.0      
 [4] modelr_0.1.9         bslib_0.4.0          microbenchmark_1.4.9
 [7] assertthat_0.2.1     highr_0.9            googlesheets4_1.0.1 
[10] cellranger_1.1.0     yaml_2.3.5           pillar_1.8.1        
[13] backports_1.4.1      lattice_0.20-44      glue_1.6.2          
[16] digest_0.6.29        rvest_1.0.3          colorspace_2.0-3    
[19] htmltools_0.5.3      Matrix_1.5-1         pkgconfig_2.0.3     
[22] broom_1.0.1          haven_2.5.1          scales_1.2.1        
[25] tzdb_0.3.0           googledrive_2.0.0    farver_2.1.1        
[28] generics_0.1.3       ellipsis_0.3.2       cachem_1.0.6        
[31] withr_2.5.0          cli_3.4.0            magrittr_2.0.3      
[34] crayon_1.5.1         readxl_1.4.1         evaluate_0.16       
[37] fs_1.5.2             fansi_1.0.3          MASS_7.3-54         
[40] xml2_1.3.3           hms_1.1.2            gargle_1.2.1        
[43] lifecycle_1.0.2      munsell_0.5.0        reprex_2.0.2        
[46] zip_2.2.1            compiler_4.1.1       Deriv_4.1.3         
[49] jquerylib_0.1.4      rlang_1.0.5          grid_4.1.1          
[52] rstudioapi_0.14      labeling_0.4.2       rmarkdown_2.16      
[55] gtable_0.3.1         DBI_1.1.3            R6_2.5.1            
[58] lubridate_1.8.0      knitr_1.40           fastmap_1.1.0       
[61] utf8_1.2.2           stringi_1.7.8        Rcpp_1.0.9          
[64] vctrs_0.4.1          dbplyr_2.2.1         tidyselect_1.1.2    
[67] xfun_0.33           

RStudio version 1.3.1073.

8 Cite R packages used

for (i in pack_to_load) print(citation(i), bibtex = FALSE)

To cite package 'R.utils' in publications use:

  Henrik Bengtsson (2022). R.utils: Various Programming Utilities. R
  package version 2.12.0. https://CRAN.R-project.org/package=R.utils


The 'tools' package is part of R.  To cite R in publications use:

  R Core Team (2021). R: A language and environment for statistical
  computing. R Foundation for Statistical Computing, Vienna, Austria.
  URL https://www.R-project.org/.

We have invested a lot of time and effort in creating R, please cite it
when using it for data analysis. See also 'citation("pkgname")' for
citing R packages.


To cite package 'openxlsx' in publications use:

  Philipp Schauberger and Alexander Walker (2021). openxlsx: Read,
  Write and Edit xlsx Files. R package version 4.2.5.
  https://CRAN.R-project.org/package=openxlsx


To cite ggplot2 in publications, please use:

  H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
  Springer-Verlag New York, 2016.


Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R,
Grolemund G, Hayes A, Henry L, Hester J, Kuhn M, Pedersen TL, Miller E,
Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V, Takahashi
K, Vaughan D, Wilke C, Woo K, Yutani H (2019). "Welcome to the
tidyverse." _Journal of Open Source Software_, *4*(43), 1686. doi:
10.21105/joss.01686 (URL: https://doi.org/10.21105/joss.01686).


To cite package 'patchwork' in publications use:

  Thomas Lin Pedersen (2022). patchwork: The Composer of Plots. R
  package version 1.1.2. https://CRAN.R-project.org/package=patchwork


To cite package 'doBy' in publications use:

  Søren Højsgaard and Ulrich Halekoh (2022). doBy: Groupwise
  Statistics, LSmeans, Linear Estimates, Utilities. R package version
  4.6.13. https://CRAN.R-project.org/package=doBy

ATTENTION: This citation information has been auto-generated from the
package DESCRIPTION file and may need manual editing, see
'help("citation")'.


To cite package 'ggrepel' in publications use:

  Kamil Slowikowski (2021). ggrepel: Automatically Position
  Non-Overlapping Text Labels with 'ggplot2'. R package version 0.9.1.
  https://CRAN.R-project.org/package=ggrepel

END OF SCRIPT